package com.tjy.wfx.service.impl;

import com.tjy.wfx.dao.SysModuleMapper;
import com.tjy.wfx.dao.SysRoleFunMapper;
import com.tjy.wfx.model.SysModule;
import com.tjy.wfx.model.SysRoleFun;
import com.tjy.wfx.service.SysModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class SysModuleServiceImpl implements SysModuleService {
@Autowired
private SysModuleMapper sysModuleMapper;
@Autowired
private SysRoleFunMapper sysRoleFunMapper;
    @Override
    public List<SysModule> findAll() {
        return sysModuleMapper.selectAll();
    }

    @Override
    public List<SysModule> findRoleMenu(String roleCode) {
        //1.查询所有的菜单信息
        List<SysModule> moduleList = sysModuleMapper.selectAll();
        //2.根据角色id查询对应的菜单id
        Example example = new Example(SysRoleFun.class);
        Example.Criteria and = example.and();
        and.andEqualTo("roleId",roleCode);
        List<SysRoleFun> roleFunList = sysRoleFunMapper.selectByExample(example);
        //3.查询 菜单信息中的id 和 角色菜单信息中的菜单id 作匹配 相等就把菜单信息中的checked值设置为true open值也是true
        for (SysModule module : moduleList) {
            for (SysRoleFun roleFun : roleFunList) {
                if(module.getModuleCode().equals(roleFun.getModuleId())){
                    module.setChecked(true);
                    module.setOpen(true);
                    //如果匹配成功就跳出循环
                    break;
                }

            }
        }
        return moduleList;
    }
}
